package com.cwd.arm.inter.controller;

import com.cwd.arm.application.dto.DictDto;
import com.cwd.arm.application.service.read.facade.DictReadService;
import com.cwd.arm.inter.vo.ResponseVo;
import com.cwd.arm.inter.vo.dict.DictVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:数据字典
 * Created on 2021年11月19日
 *
 * @author yan
 */
@Api(value = "数据字典接口", description = "数据字典接口")
@RestController
@RequestMapping(value = "/dict")
public class DictController {

    @Autowired
    private DictReadService readService;

    @ApiOperation(value = "查询数据字典信息", notes =
            "参数type取:" +
                    "项目类型:PROJECT_TYPE;" +
                    "业务类型:BUSINESS_TYPE;" +
                    "项目状态:PROJECT_STATUS"+
                    "支付方式:PAY_TYPE"+
                    "承兑类型:HONOR_PAY_TYPE"+
                    "年份:FY"+
                    "收款计划月份:MONTH"+
                    "收款节点:NODE_TYPE"+
                    "消息类型:MSG_TYPE"
    )
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "type", value = "数据字典类型", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/{type}")
    public ResponseVo<List<DictVo>> getDictList(@PathVariable String type) {
        List<DictDto> dtoList = readService.selectList(type);
        return ResponseVo.<List<DictVo>>BUILDER().success(convertToVoList(dtoList));
    }

    private DictVo convertToVo(DictDto dto) {
        DictVo vo = new DictVo();
        BeanUtils.copyProperties(dto, vo);
        return vo;
    }

    private List<DictVo> convertToVoList(List<DictDto> dictDtoList) {
        return dictDtoList.stream().map(dto -> convertToVo(dto)).collect(Collectors.toList());
    }

}
